/* eslint-disable object-shorthand,spaced-comment,quotes,dot-notation,semi-spacing,no-var,linebreak-style,keyword-spacing,no-array-constructor,key-spacing,space-before-blocks,space-unary-ops */
/**
 * Created by asus on 2017/8/29.
 */
const columnTable = {
  treeToColumn: function(param){
    var result = {
      rowNum:0,  //记录指标或分组占用的行数 用于数据表的行数
      colNum:0,  //记录指标或分组占用的列数 用于交叉点表的列数
      colInfo:[] //记录分组或指标的信息[{rowIndex:,info:},{rowIndex:,info:}] 用于数据表的表格信息标识，行方向的指标或分组信息
    };
    var colInfo = new Array();
    //参数检查
    if(!param || !param["table"] || !param["data"]){
      alert('参数错误{data:a,colMax:5,table:$("#myTable")}');
      return ;
    }
    //要插入表格的树json
    var data = param["data"];
    //取得table对象
    var table = param["table"];
    // console.log(table,'column')
    //这是表格头部封装！！！！！！！
    //格式化json树,为每个节点添加上等级
    this.formatDataLevel(data);
    //格式化json树,为每个节点添加上宽度
    this.formatDataWidth(data);
    //取得树的深度，即树占用的行数
    var rowMax = 0, colMax = 0;
    colMax = this.getWidth_columnTable(data);
    //取的树的宽度，即树占用的最大列数
    rowMax = this.getDeepth_columnTable(data);
    var root = {
      level:0,
      width:colMax,
      cat_id:0,
      sub:data
    };
    //根据等级构造行
    var colIdx = 0;
    var cindex = 0; //记录实际应在单元格
    for(var rowIdx = 0; rowIdx < rowMax ; rowIdx ++){
      colIdx = 0;
      var level = rowIdx + 1;
      cindex = 0;
      var trS = getTr(root,level,rowIdx);
      //补齐后缺失单元格
      // if(cindex < colMax){
      //     var behindMiss = "";
      //     var  num = colMax - cindex;
      //     for(var j = 0 ; j < num ;j ++){
      //         behindMiss += "<td style = \'height: 22px;width: 100px;border: 1px solid black;background-color: grey\' rowidx = '"+rowIdx+"' colidx = '"+colIdx+"' cindex='"+cindex+"'></td>";
      //         colIdx ++;
      //         cindex ++;
      //     }
      //     trS = trS.replace("</tr>",behindMiss + "</tr>")
      // }
      $(trS).appendTo(table);
    }
    var tdStr = "";
    function getTr(node,level,rowIdx) {
      tdStr = "";
      getTd(node,level,rowIdx,"");
      var trStr = "<tr rowidx = \""+rowIdx+"\">" + tdStr + "</tr>";
      return trStr;
    }
    function getTd(data,level,rowIdx){
      if ( data.sub.length == 0)
      {
        return ;
      } else {
        for ( var i = 0;i < data.sub.length;i ++){
          
          //记录指标和分组的层级关系path
          if(data.attribute){
              if(data.attribute.ig_type === "iden"){
                data.sub[i].attribute.path = data.attribute.path + "-" + data.sub[i].attribute.idenCode;
                data.sub[i].attribute.pathName = data.attribute.pathName + "-" + data.sub[i].attribute.idenName;
              }else if(data.attribute.ig_type === "group"){
                data.sub[i].attribute.path = data.attribute.path + "-" + data.sub[i].attribute.groupCode;
                data.sub[i].attribute.pathName = data.attribute.pathName + "-" + data.sub[i].attribute.groupName;
              }
          }else{
              if(data.sub[i].attribute.ig_type === "iden"){
                data.sub[i].attribute.path =  data.sub[i].attribute.idenCode;
                data.sub[i].attribute.pathName =  data.sub[i].attribute.idenName;
              }else if(data.sub[i].attribute.ig_type === "group"){
                data.sub[i].attribute.path =  data.sub[i].attribute.groupCode;
                data.sub[i].attribute.pathName =  data.sub[i].attribute.groupName;
              }
          }

          if(data.sub[i].level == level){
            var rowspanStr = "";
            if(data.sub[i].sub.length == 0){

              //加上报告期相关信息
              if(data.attribute){
                if(data.attribute.isPeriod != undefined && data.attribute.isPeriod !=null && data.attribute.isPeriod === true){
                  var periods = data.attribute.groupCode;
                  if(data.sub[i].attribute.isPeriod != undefined && data.sub[i].attribute.isPeriod != null && data.sub[i].attribute.isPeriod === false){
                    data.sub[i].attribute.period = periods;
                  }
                }
              }
              if(data.sub[i].level < rowMax){
                rowspanStr += " rowspan = '" + (rowMax - data.sub[i].level+1)+"'";
              }
            }
            tdStr += "<td style='width: 100px;height: 35px;' colspan='"+data.sub[i].width+"' rowidx = '"+rowIdx+"' colidx = '"+colIdx+"' cindex='"+cindex+"' "+rowspanStr+" title='" + data.sub[i].name + "'><span>" + data.sub[i].name + "</span></td>";
            data.sub[i].cindex = cindex;
            colIdx ++;
            cindex += data.sub[i].width;
          }
          if(data.sub[i].sub.length == 0){
            var info = {
              index:colIdx,
              info:data.sub[i].attribute
            };
            colInfo.push(info);
          }
          getTd(data.sub[i],level,rowIdx) ;
        }
      }
    }
    //构建返回对象
    result.rowNum = rowMax;
    result.colNum = colMax;
    result.colInfo = colInfo;
    return result;
  },
  //格式化数据，为各节点添加上等级
  formatDataLevel: function (data) {
    //获得json树的深度
    var ans = 0;
    var getColDeepth = function (data) {
      //计算出各指标树或分组树的最大值
      var maxRow = 0;
      for(var ddi = 0;ddi < data.length ;ddi ++){
        dfs(data[ddi],1);
        maxRow = Math.max(ans,maxRow);
      }
      return maxRow;
    }
    //递归计算树的深度
    var dfs = function(data ,deep){
      if ( data.sub.length == 0 )
      {
        ans = Math.max(ans , deep ) ;
        data.level = deep;
        return ;
      }
      else {
        for ( var i = 0;i < data.sub.length;i ++){
          dfs(data.sub[i] , deep + 1) ;
        }
        data.level = deep;
      }
    }
    getColDeepth(data);
  },
  //格式化数据为每个节点添加上节点的宽度
  formatDataWidth: function (data) {
    //递归计算每个节点的宽度
    var widthN = 0;
    var  getWidthNum = function (node){
      if ( node.sub.length == 0 )
      {
        widthN ++;
        return ;
      } else {
        for (var i = 0;i < node.sub.length;i ++){
          getWidthNum(node.sub[i]) ;
        }
      }
    }
    //递归每个节点
    var  recursiveData = function (node){
      if ( node.sub.length == 0 )
      {
        node.width = 0;
        return ;
      } else {
        for (var i = 0;i < node.sub.length;i ++){
          recursiveData(node.sub[i]);
          getWidthNum(node.sub[i]) ;
          node.sub[i].width = widthN;
          widthN = 0;
        }
      }
    }

    for(var i = 0 ; i < data.length ; i ++){
      widthN = 0;
      recursiveData(data[i]);
      getWidthNum(data[i]);
      data[i].width = widthN;
    }
  },
  //取得json树到占用的行数
  getDeepth_columnTable: function (data) {
    var maxRow = 0;
    var ans = 0;
    //递归计算树的深度
    var dfs = function(data ,deep){
      if ( data.sub.length == 0 )
      {
        ans = Math.max(ans , deep ) ;
        data.level = deep;
        return ;
      }
      else {
        for ( var i = 0;i < data.sub.length;i ++)
          dfs(data.sub[i] , deep + 1) ;
        data.level = deep;
      }
    }
    for(var i = 0; i < data.length ; i ++){
      dfs(data[i],1);
      maxRow = Math.max(ans,maxRow);
    }
    return maxRow;
  },
  //取的json树总共占用的列数
  getWidth_columnTable: function (data) {
    var value = 0;
    //递归计算宽度
    var  getWidthNum = function (node){
      if ( node.sub.length == 0 )
      {
        value ++;
        return ;
      } else {
        for (var i = 0;i < node.sub.length;i ++){
          getWidthNum(node.sub[i]) ;
        }
      }
    }
    for(var i = 0 ; i < data.length ;i ++){
      getWidthNum(data[i]);
    }
    return value;
  }
}
export default columnTable


